//package ioio.ioiorobotics.rotatingsensor;
//
//import android.app.Activity;
//import android.os.Bundle;
//
//public class RotatingSensorActivity extends Activity {
//    /** Called when the activity is first created. */
//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);
//    }
//}

package ioio.ioiorobotics.rotatingsensor;

import ioio.ioiorobotics.rotatingsensor.R;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.PwmOutput;
import ioio.lib.api.AnalogInput;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.AbstractIOIOActivity;
import android.os.Bundle;
import android.widget.ToggleButton;
//import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
//import android.widget.Toast;
//import android.view.View;
//import android.view.View.OnTouchListener;


/**
 * This is the main activity of the HelloIOIO example application.
 * 
 * It displays a toggle button on the screen, which enables control of the
 * on-board LED. This example shows a very simple usage of the IOIO, by using
 * the {@link AbstractIOIOActivity} class. For a more advanced use case, see the
 * HelloIOIOPower example.
 */
public class RotatingSensorActivity extends AbstractIOIOActivity {
//	private ToggleButton ledButton_;
	private 
		SeekBar _seekBarVar;
		TextView servoVarField;
		TextView sharpIRVarField;
		ToggleButton sweepSensorToggleButton;
		volatile float servoVarValue;
		volatile float volts;
		
	
	/**
	 * Called when the activity is first created. Here we normally initialize
	 * our GUI.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		_seekBarVar = (SeekBar) findViewById(R.id.seekBarVar);
		servoVarField = (TextView) findViewById(R.id.textViewVarVal);
		sharpIRVarField = (TextView) findViewById(R.id.sharpIRVarField);
		sweepSensorToggleButton = (ToggleButton) findViewById(R.id.sweepSensorToggleButton);
		_seekBarVar.setMax(100);
		_seekBarVar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {}
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {}
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				servoVarValue = (float)progress / (float)_seekBarVar.getMax();
				servoVarField.setText(String.valueOf(servoVarValue)); 
				//sharpIRVarField.setText(String.valueOf(volts));				
			}
		});
	}
//
//	public void MessageBox(String message){
//	    Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
//	}
//	
//	// TestSevo
//	public void testServoCallback(View v){
//		MessageBox("testServoToggleButton");
//	}
//	
	/**
	 * This is the thread on which all the IOIO activity happens. It will be run
	 * every time the application is resumed and aborted when it is paused. The
	 * method setup() will be called right after a connection with the IOIO has
	 * been established (which might happen several times!). Then, loop() will
	 * be called repetitively until the IOIO gets disconnected.
	 */
	class IOIOThread extends AbstractIOIOActivity.IOIOThread {
		/** The on-board LED. */
		private 
			PwmOutput led_;
			PwmOutput servo_;
			AnalogInput sharpIRin_; 

		/**
		 * Called every time a connection with IOIO has been established.
		 * Typically used to open pins.
		 * 
		 * @throws ConnectionLostException
		 *             When IOIO connection is lost.
		 * 
		 * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#setup()
		 */
		@Override
		protected void setup() throws ConnectionLostException {			
			try {
				led_ = ioio_.openPwmOutput(0, 300);
				servo_ = ioio_.openPwmOutput(3, 50);
				sharpIRin_ = ioio_.openAnalogInput(46);
				enableUi(true);
			} catch (ConnectionLostException e) {
				enableUi(false);
				throw e;
			}
		}

		/**
		 * Called repetitively while the IOIO is connected.
		 * 
		 * @throws ConnectionLostException
		 *             When IOIO connection is lost.
		 * 
		 * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#loop()
		 */
		@Override
		protected void loop() throws ConnectionLostException {					
			try {
				// Parallax standard servo 0.75 ms - >  2.25 m (-90->+90)
				if(sweepSensorToggleButton.isChecked())
				{
					servoVarValue = servoVarValue + 0.01f;
					if (servoVarValue>1)
					{
						servoVarValue = 0;
					}
				}
				
				servo_.setPulseWidth(750f + servoVarValue*(2250f-750f));
				
				setTextSharpIR(String.valueOf(sharpIRin_.getVoltage()));				
				setTextServo(String.valueOf(servoVarValue));
				
				led_.setDutyCycle(1 - servoVarValue);	        
				sleep(10);
			} catch (InterruptedException e) {
				ioio_.disconnect();
				led_.setDutyCycle(1);
			} catch (ConnectionLostException e) {
				//enableUi(false);
				throw e;
			}
		}
	}

	/**
	 * A method to create our IOIO thread.
	 * 
	 * @see ioio.lib.util.AbstractIOIOActivity#createIOIOThread()
	 */
	@Override
	protected AbstractIOIOActivity.IOIOThread createIOIOThread() {
		return new IOIOThread();
	}
	
	private void enableUi(final boolean enable) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				_seekBarVar.setEnabled(enable);
			}
		});
	}
	
	private void setTextServo(final String str1) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				servoVarField.setText(str1);
			}
		});
	}
	
	private void setTextSharpIR (final String str1) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				sharpIRVarField.setText(str1);
			}
		});
	}
}